﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using BooksOnline.Models.Entities;
using BooksOnline.Repositories;
using NHibernate;
using NUnit.Framework;
using NHibernate.Tool.hbm2ddl;

namespace BooksOnline.Tests
{
    [TestFixture]
    public class BookRepositoryFixture : FixtureBase<Book>
    {
        Book expectedBook = new Book();

        [TestFixtureSetUp]
        protected override void SetUp()
        {
            expectedBook.Id = Guid.NewGuid();
            expectedBook.ISBN = "ISBN";
            expectedBook.Price = 12;
            expectedBook.Title = "Title 1";
            expectedBook.Author = new BookAuthor() { Id = Guid.NewGuid(), Name = "Author 1" };
            expectedBook.CreateTrackingTimestamp = new CreateTracking() { CreatedBy = Guid.NewGuid(), CreatedOn = DateTime.Now };
            expectedBook.ChangeTrackingTimestamp = null;

            Session.Save(expectedBook.Author);
            Session.Save(expectedBook);
            Session.Flush();

            base.SetUp();
        }

        [Test]
        public void ById_works_fine()
        {
            var bookRepository = new BookRepository();
            var actualBook = bookRepository.ById(expectedBook.Id);

            Assert.IsNotNull(actualBook);
            Assert.AreEqual(actualBook.Id, expectedBook.Id);
        }

        [Test]
        public void ByTitle_works_fine()
        {
            var bookRepository = new BookRepository();
            var actualBook = bookRepository.ByTitle("Title%");

            Assert.IsNotNull(actualBook);
            Assert.IsTrue(actualBook.Count > 0);
        }

        [Test]
        public void ByAuthor_works_fine()
        {
            var bookRepository = new BookRepository();
            var actualBook = bookRepository.ByAuthor("Author%");

            Assert.IsNotNull(actualBook);
            Assert.IsTrue(actualBook.Count > 0);
        }
    }
}
